package com.db.dao;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.junit.Test;


import com.db.bean.User;

import util.BaseDAO;
import util.JDBCUtiles;

/**
* @author 作者:秋哲    E-mail:2810201146@qq.com:
* @version 创建时间：2020年5月21日 下午5:47:54
* @description: 接口实现类
*/
public class UserDAOImpl extends BaseDAO<User> implements UserDAO {
	
	
	@Override
	public int insert(User user_obj) {
		String sql = "insert into user_user VALUES(?,?,?,?,?,?)";
		int update = update(sql, user_obj.getId(),user_obj.getUsername() ,user_obj.getPassword(),user_obj.getName(),user_obj.getRole(),user_obj.getPresident_id());
		return update;
	}
	
	public static void main(String[] args) throws Exception {
		
		File file = new File("json.json");
		String string = FileUtils.readFileToString(file,"utf8");
		
//		JSON
//		JSONArray object = JSON.parseArray(string);
//		Connection conn = JDBCUtiles.getConnection();
//		int n = 6;
//		for (Object object2 : object) {
//			JSONObject o = JSON.parseObject(object2.toString());
//			User user = new User(n++, o.get("username").toString(), o.get("password").toString(), o.get("name").toString(), o.get("role").toString(), 1);
//			System.out.println(user.toString());
//			new UserDAOImpl().insert(user);
//		}
//		conn.commit();
//		JDBCUtiles.closeResource(conn);
		
	}
	
	
	@Override
	public int deletById(int user_id) {
		String sql = "DELETE FROM user_user WHERE id = ?";
		int n = update(sql,user_id);
		return n;
	}

	@Override
	public int updateUser(User user_obj) {
		String sql = "update user_user SET `username`=?,`password`=?,`name`=?,`role`=?,`president_id`=? where id = ?";
		int update = update(sql, user_obj.getUsername() ,user_obj.getPassword(),user_obj.getName(),user_obj.getRole(),user_obj.getPresident_id(),user_obj.getId());
		return update;
	}



	@Override
	public List<User> getPresidentUserAll(int president_id) {
		String sql = "SELECT * from user_user where president_id = ?";
		List<User> queryList = queryList(sql, president_id);
		return queryList;
	}

	@Override
	public User getUser(int user_id) {
		String sql = "select * from user_user where id = ?";
		User query = query(sql, user_id);
		return query;
	}
	
	@Override
	public User getUser(String username, String password) {
		String sql = "select * from user_user where username = ? and password = ?";
		User query = query(sql, username,password);
		return query;
	}

}
